package org.swim.servlet;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.Ability;
import org.swim.entity.User;
import org.swim.session.FriendshipManagerRemote;
import org.swim.session.HelpRequestManagerRemote;
import org.swim.session.UserManagerRemote;

/**
 * Servlet implementation class SendHelpRequest
 */
public class SendHelpRequest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendHelpRequest() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User me = (User) session.getAttribute("user");
        int myId = me.getId();
        int himId = Integer.parseInt(request.getParameter("user")); 
        InitialContext ic;
        try {
 			ic = new InitialContext();
 			FriendshipManagerRemote friendshipManager = (FriendshipManagerRemote)ic.lookup("FriendshipManagerRemote");    
 			if(friendshipManager.areFriend(myId, himId)==false) {
 				request.setAttribute("rights", false);
 			} else {
 				request.setAttribute("rights", true);
 				UserManagerRemote userManager = (UserManagerRemote) ic.lookup("UserManagerRemote");
 				User user = userManager.getUser(himId);
 				List<Ability> abilities = userManager.getAbilityListByID(himId);
 				request.setAttribute("user", user);
 				request.setAttribute("userabilities", abilities);
 			}
        } catch (NamingException e) {
 			e.printStackTrace();
 		} 	
        request.getRequestDispatcher("WEB-INF/HelpRequest.jsp").forward(request, response);
     }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        InitialContext ic;
        HttpSession session = request.getSession();
        User me = (User) session.getAttribute("user");
        int him = Integer.parseInt(request.getParameter("user"));
        int ability = Integer.parseInt(request.getParameter("ability"));
        String text = request.getParameter("text");
        try {
 			ic = new InitialContext();
			HelpRequestManagerRemote helpManager = (HelpRequestManagerRemote) ic.lookup("HelpRequestManagerRemote");
			helpManager.createNewHelpRequest(me.getId(), him, ability, text);
        } catch (NamingException e) {
 			e.printStackTrace();
 		} 
		request.setAttribute("rights", true);
        request.getRequestDispatcher("Profile?id=" + him).forward(request, response);
	}

}
